package com.xilihui.xlh.business.activitys.store;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.RefundEntity;
import com.xilihui.xlh.business.entities.StoreGoodDetailsEntity;
import com.xilihui.xlh.business.requests.StoreRequest;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.feezu.liuli.timeselector.Utils.TextUtil;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/10/8.
 */

public class AlterationActivity extends BaseCompatActivity{
    
    @BindView(R.id.sdv)
    SimpleDraweeView sdv;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_model)
    TextView tv_model;
    
    @BindView(R.id.et_beizhu)
    EditText et_beizhu;
    @BindView(R.id.tv_jiner)
    TextView tv_jiner;
    
    @BindView(R.id.tv_submit)
    TextView tv_submit;
    
    
    String order_id = "";
    String status = "";
    RefundEntity refundEntity;
    StoreGoodDetailsEntity storeGoodDetailsEntity;
    
    
    @Override
    public int getLayout() {
        return R.layout.activity_alteration;
    }

    @Override
    public String initTitle() {
        return "仅退款";
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        
        order_id = getIntent().getStringExtra("order_id");
        status = getIntent().getStringExtra("status");
        storeGoodDetailsEntity = (StoreGoodDetailsEntity) getIntent().getSerializableExtra("data");
        
        tv_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submit();
            }
        });
        
        sdv.setImageURI(UrlConst.baseUrl()+storeGoodDetailsEntity.getOrder_info().getOrder().get(0).getOrder_goods().getOriginal_img());
        tv_title.setText(storeGoodDetailsEntity.getOrder_info().getOrder().get(0).getOrder_goods().getGoods_name());
        tv_model.setText(storeGoodDetailsEntity.getOrder_info().getOrder().get(0).getOrder_goods().getSpec_key_name());
        tv_jiner.setText("￥"+storeGoodDetailsEntity.getOrder_info().getOrder().get(0).getOrder_amount());
        
        if(storeGoodDetailsEntity.getOrder_info().getOrder().get(0).getShipping_status().equals("0")){
            setTitle("仅退款");
        }else {
            setTitle("退货退款");
        }
        
    }

    @Override
    public void initData() {
//        getData();
    }
    
    public void getData(){
        StoreRequest.returnGoods(this,order_id)
                .compose(DoTransform.<RefundEntity>applyScheduler(this,true))
                .compose(this.<RefundEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<RefundEntity>(this) {
                    @Override
                    protected void onSuccess(RefundEntity entity) {
                        LogUtil.i("mylog","url:"+UrlConst.baseUrl()+entity.getInfo().getOrder_goods().getOriginal_img());
                        sdv.setImageURI(UrlConst.baseUrl()+entity.getInfo().getOrder_goods().getOriginal_img());
                        tv_title.setText(entity.getInfo().getOrder_goods().getGoods_name());
                        tv_model.setText(entity.getInfo().getOrder_goods().getSpec_key_name());
                        
                    }
                    
                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
        
    }
    
    
    public void submit(){
        String content = et_beizhu.getText().toString();
        if(TextUtil.isEmpty(content)){
            content = "";
        }
        
        StoreRequest.applyReturn(this,order_id,content)
                .compose(DoTransform.<BaseEntity>applyScheduler(this,true))
                .compose(this.<BaseEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<BaseEntity>(this) {
                    @Override
                    protected void onSuccess(BaseEntity entity) {
                        ToastUtil.toastShortPositive(entity.getMsg());
                        finish();
                        YEventBuses.post(new YEventBuses.Event("revocation"));
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
        
    }
    
    
    
}
